﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        string ImagePath = "";
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Create single instance of sample data from first line of dataset for model input
            var imageBytes = File.ReadAllBytes(ImagePath);
            MLModel1.ModelInput sampleData = new MLModel1.ModelInput()
            {
                ImageSource = imageBytes,
            };
            // Make a single prediction on the sample data and print results.
            var sortedScoresWithLabel = MLModel1.PredictAllLabels(sampleData);
            var scoreModel = sortedScoresWithLabel.OrderByDescending(f=>f.Value).FirstOrDefault();
            textBox1.Text = $"分类:{scoreModel.Key}  分数:{scoreModel.Value};";
        }
        /// <summary>
        /// 选择图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "请选择一个图片";
            openFileDialog.Multiselect = false;
            openFileDialog.Filter = "*.jpg|*.png|*.jpge|*.pnge";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                //获取用户文件路径
                 var filePath =   openFileDialog.FileName;
                ImagePath = filePath;   
                using (var ms = new System.IO.MemoryStream(System.IO.File.ReadAllBytes(filePath)))
                {
                    pictureBox1.Image = System.Drawing.Image.FromStream(ms);
                }
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }
    }
}
